package com.usian.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.behavior.mapper.ApUnlikesBehaviorMapper;
import com.usian.behavior.service.ApBehaviorEntryService;
import com.usian.behavior.service.ApUnLikesBehaviorService;
import com.usian.model.article.dtos.ArticleInfoDto;
import com.usian.model.behavior.dtos.UnLikesBehaviorDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApUnlikesBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * ClassName: ApUnLikesBehaviorServiceImpl
 * Author:   WGG
 * Date:    2022-12-2022/12/1-20:24
 * Version: 1.0
 * Description:
 */
@Service
public class ApUnLikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper,ApUnlikesBehavior> implements ApUnLikesBehaviorService {

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;


    @Override
    public ResponseResult unlikesBehavior(UnLikesBehaviorDto dto) {
        //判空
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取登录id
        ApUser user = AppThreadLocalUtils.getUser();
        if (user.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.TOKEN_EXPIRE);
        }
        //判断该行为实体是否存在
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.showBehavior(user.getId(), null);
        if (apBehaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断该条数据是否存在
        ApUnlikesBehavior unlikesBehavior = getOne(Wrappers.<ApUnlikesBehavior>lambdaQuery().eq(ApUnlikesBehavior::getEntryId, apBehaviorEntry.getId()));
        if (unlikesBehavior == null) {
            //不存在
            //封装数据
            ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
            apUnlikesBehavior.setArticleId(dto.getArticleId());
            apUnlikesBehavior.setCreatedTime(new Date());
            apUnlikesBehavior.setEntryId(apBehaviorEntry.getId());
            apUnlikesBehavior.setType(dto.getType());
            //添加
            save(apUnlikesBehavior);
        } else {
            //存在 修改
            update(Wrappers.<ApUnlikesBehavior>lambdaUpdate()
                    .set(ApUnlikesBehavior::getType, dto.getType())
                    .eq(ApUnlikesBehavior::getId, unlikesBehavior.getId())
            );

        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ApUnlikesBehavior showUnlikesBehavior(ArticleInfoDto dto) {
        if (dto != null){
           return getOne(Wrappers.<ApUnlikesBehavior>lambdaQuery().eq(ApUnlikesBehavior::getArticleId,dto.getArticleId()).eq(ApUnlikesBehavior::getEntryId,dto.getId()));
        }
        return null;
    }
}
